import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedHashMap;
import java.util.Map;

public class MyFrame extends JFrame {
    public MyFrame() {
        /* 初始化各元器件 */
        initComponents();
    }

    /* 初始化元器件函数 */
    private void initComponents() {
        // region 窗口初始化部分
        /* 设置窗口标题 */
        setTitle("Workshop Check in");
        /* 设置窗口最小尺寸 */
        setMinimumSize(new Dimension(450, 600));
        /* 设置窗口布局格式 frameContent:窗口内容容器 */
        var frameContent = getContentPane();
        frameContent.setLayout(new BorderLayout());
        /* 设置默认关闭 */
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        /* 设置窗口位置 */
        setLocation(100, 100);
        // endregion

        // region 元件初始化部分

        /* 主题区域 */
        JPanel panelMain = new JPanel();

        /* 标题区域 */
        JLabel labelTitle = new JLabel();

        /* 姓名输入区域 */
        JPanel panelName = new JPanel();
        /* 姓输入区域 */
        JPanel panelFirstName = new JPanel();
        /* 姓输入框 */
        JTextField textFieldFirstName = new JTextField(10);
        /* 名输入区域 */
        JPanel panelSecondName = new JPanel();
        /* 名输入框 */
        JTextField textFieldSecondName = new JTextField(10);

        /* 出生日期选择区域 */
        JPanel panelDateBirth = new JPanel();
        /* 出生月选择区域 */
        JPanel panelDateMonth = new JPanel();
        /* 出生月选择框 */
        JComboBox<String> comboBoxMonth = new JComboBox<>();
        /* 出生年选择区域 */
        JPanel panelDateYear = new JPanel();
        /* 出生年选择框 */
        JComboBox<String> comboBoxYear = new JComboBox<>();

        /* 课程选择区域 */
        JPanel panelProgramme = new JPanel();
        /* 课程名称选择区域 */
        JPanel panelProgrammeName = new JPanel();
        /* 课程名称选择框 */
        JComboBox<String> comboBoxProgrammeName = new JComboBox<>();
        /* 课程学期选择区域 */
        JPanel panelTerm = new JPanel();
        /* 课程学期选择框 */
        JComboBox<String> comboBoxTerm = new JComboBox<>();

        /* 邮箱输入区域 */
        JPanel panelEmail = new JPanel();
        /* 邮箱输入框 */
        JTextField textFieldEmail = new JTextField(20);

        /* 手机号输入区域 */
        JPanel panelPhone = new JPanel();
        /* 手机号输入框 */
        JTextField textFieldPhone = new JTextField(20);

        /* 手机号输入区域 */
        JPanel panelBRC = new JPanel();
        /* 手机号输入框 */
        JTextField textFieldBRC = new JTextField(5);

        /* 确认框区域 */
        JPanel panelConfirm = new JPanel();
        /* 确认框RA区域 */
        JPanel panelRA = new JPanel();
        /* 确认框CI区域 */
        JPanel panelCI = new JPanel();
        /* 确认框RA */
        JCheckBox checkBoxRA = new JCheckBox();
        /* 确认框CI */
        JCheckBox checkBoxCI = new JCheckBox();

        /* 按钮区域 */
        JPanel panelBtn = new JPanel();
        /* 按钮NS */
        JButton buttonNS = new JButton();
        /* 按钮C */
        JButton buttonC = new JButton();
        /* 按钮E */
        JButton buttonE = new JButton();

        // endregion

        // region panelMain设置区域
        /* 设置panelMain为8行1列网格布局 */
        panelMain.setLayout(new GridLayout(9, 1));
        //endregion

        // region labelTitle设置区域 (第一行)
        /* 设置标题内容文字 */
        labelTitle.setText("Please complete the information below:");
        /* 设置文字垂直居中 */
        labelTitle.setHorizontalAlignment(JLabel.CENTER);
        /* 设置字体格式 */
        labelTitle.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 18));
        /* 添加到主区域 */
        panelMain.add(labelTitle);
        // endregion

        // region panelFirstName设置区域 (第二行第一列)

        /* 设置带标题边框 */
        textFieldFirstName.setBorder(new TitledBorder("First Name"));
        /* 设置区域为边界布局 */
        panelFirstName.setLayout(new BorderLayout());
        /* 添加到姓输入区域东侧 */
        panelFirstName.add(textFieldFirstName, BorderLayout.EAST);

        // endregion

        // region panelSecondName设置区域 (第二行第二列)

        /* 设置带标题边框 */
        textFieldSecondName.setBorder(new TitledBorder("Second Name"));
        /* 设置区域为边界布局 */
        panelSecondName.setLayout(new BorderLayout());
        /* 添加到名输入区西侧 */
        panelSecondName.add(textFieldSecondName, BorderLayout.WEST);


        // endregion

        // region panelName设置区域 (第二行)

        /* 设置带标题边框 */
        panelName.setBorder(new TitledBorder("Name"));
        /* 设置为网格布局 */
        panelName.setLayout(new GridLayout(1, 2, 10, 0));

        /* 添加姓输入区域 */
        panelName.add(panelFirstName);
        /* 添加名输入区域 */
        panelName.add(panelSecondName);
        /* 添加到主区域 */
        panelMain.add(panelName);

        // endregion

        // region panelDateMonth设置区域 (第三行第一列)

        /* 设置带标题边框 */
        comboBoxMonth.setBorder(new TitledBorder("Month"));
        /* 设置下拉框大小 */
        comboBoxMonth.setPreferredSize(new Dimension(100, 10));
        /* 设置下拉框内容 */
        comboBoxMonth.setModel(new DefaultComboBoxModel<>(new String[]{
                "January",
                "February",
                "March",
                "April",
                "May",
                "June",
                "July",
                "August",
                "September",
                "October",
                "November",
                "December"
        }));
        /* 设置区域为边界布局 */
        panelDateMonth.setLayout(new BorderLayout());
        /* 添加到月份输入区域东侧 */
        panelDateMonth.add(comboBoxMonth, BorderLayout.EAST);

        // endregion

        // region panelDateYear设置区域 (第三行第二列)

        /* 设置带标题边框 */
        comboBoxYear.setBorder(new TitledBorder("Year"));
        /* 设置下拉框大小 */
        comboBoxYear.setPreferredSize(new Dimension(100, 10));
        /* 设置下拉框内容 */
        comboBoxYear.setModel(new DefaultComboBoxModel<>(new String[]{
                "1998",
                "1999",
                "2000",
                "2001",
                "2002",
                "2003",
                "2004",
                "2005"
        }));
        /* 设置区域为边界布局 */
        panelDateYear.setLayout(new BorderLayout());
        /* 添加到月份输入区西侧 */
        panelDateYear.add(comboBoxYear, BorderLayout.WEST);

        // endregion

        // region panelDateBirth设置区域 (第三行)

        /* 设置带标题边框 */
        panelDateBirth.setBorder(new TitledBorder("Date of Birth"));
        /* 设置为网格布局 */
        panelDateBirth.setLayout(new GridLayout(1, 2, 10, 0));

        /* 添加月选择区域 */
        panelDateBirth.add(panelDateMonth);
        /* 添加年选择区域 */
        panelDateBirth.add(panelDateYear);
        /* 添加到主区域 */
        panelMain.add(panelDateBirth);

        // endregion

        // region panelProgrammeName设置区域 (第四行第一列)

        /* 设置带标题边框 */
        comboBoxProgrammeName.setBorder(new EmptyBorder(8, 1, 8, 1));
        /* 设置下拉框大小 */
        comboBoxProgrammeName.setPreferredSize(new Dimension(320, 50));
        /* 设置下拉框内容 */
        comboBoxProgrammeName.setModel(new DefaultComboBoxModel<>(new String[]{
                "Telecommunications Engineering with Management",
                "Advance Math",
                "Signals and Systems"
        }));
        /* 设置区域为边界布局 */
        panelProgrammeName.setLayout(new BorderLayout());
        /* 添加到月份输入区域东侧 */
        panelProgrammeName.add(comboBoxProgrammeName, BorderLayout.EAST);

        // endregion

        // region panelTerm设置区域 (第四行第二列)

        /* 设置带标题边框 */
        comboBoxTerm.setBorder(new EmptyBorder(8, 1, 8, 1));
        /* 设置下拉框大小 */
        comboBoxTerm.setPreferredSize(new Dimension(70, 50));
        /* 设置下拉框内容 */
        comboBoxTerm.setModel(new DefaultComboBoxModel<>(new String[]{
                "1",
                "2",
                "3",
                "4"
        }));
        /* 设置区域为边界布局 */
        panelTerm.setLayout(new BorderLayout());
        /* 添加到月份输入区域东侧 */
        panelTerm.add(comboBoxTerm, BorderLayout.EAST);

        // endregion

        // region panelProgramme设置区域 (第四行)

        /* 设置带标题边框 */
        panelProgramme.setBorder(new TitledBorder("Programme"));
        /* 设置为流式布局 */
        panelProgramme.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));

        /* 添加月选择区域 */
        panelProgramme.add(panelProgrammeName);
        /* 添加年选择区域 */
        panelProgramme.add(panelTerm);
        /* 添加到主区域 */
        panelMain.add(panelProgramme);

        // endregion

        // region panelEmail设置区域 (第五行)

        /* 设置标题边框 */
        panelEmail.setBorder(new TitledBorder("Email"));
        /* 设置区域布局 */
        panelEmail.setLayout(new FlowLayout(FlowLayout.CENTER));
        panelEmail.add(textFieldEmail);
        /* 添加到主区域 */
        panelMain.add(panelEmail);

        // endregion

        // region panelPhone设置区域 (第六行)

        /* 设置标题边框 */
        panelPhone.setBorder(new TitledBorder("Email"));
        /* 设置区域布局 */
        panelPhone.setLayout(new FlowLayout(FlowLayout.CENTER));
        panelPhone.add(textFieldPhone);
        /* 添加到主区域 */
        panelMain.add(panelPhone);

        // endregion

        // region panelBRC设置区域 (第七行)

        /* 设置标题边框 */
        panelBRC.setBorder(new TitledBorder("Booking reference code"));
        /* 设置区域布局 */
        panelBRC.setLayout(new FlowLayout(FlowLayout.CENTER));
        panelBRC.add(textFieldBRC);
        /* 添加到主区域 */
        panelMain.add(panelBRC);

        // endregion

        // region panelRA (第八行第一子行)

        /* 设置区域布局 */
        panelRA.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        /* 设置确认框RA文本 */
        checkBoxRA.setText("I have read and agree to the terms and conditions.");
        /* 添加到区域 */
        panelRA.add(checkBoxRA);

        // endregion

        // region panelRA (第八行第二子行)

        /* 设置区域布局 */
        panelCI.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        /* 设置确认框RA文本 */
        checkBoxCI.setText("I have read and agree to the terms and conditions.");
        /* 添加到区域 */
        panelCI.add(checkBoxCI);

        // endregion

        // region panelConfirm设置区域 (第八行)

        /* 设置标题边框 */
        panelConfirm.setBorder(new TitledBorder("Booking reference code"));
        /* 设置区域布局 */
        panelConfirm.setLayout(new GridLayout(2, 1, 0, 0));
        /* 设置确认框CI文本 */
        checkBoxCI.setText("I confirm that the information provided is correct");
        panelConfirm.add(panelRA);
        panelConfirm.add(panelCI);
        /* 添加到主区域 */
        panelMain.add(panelConfirm);

        // endregion

        // region panelBtn设置区域 (第九行)

        /* 设置区域布局 */
        panelBtn.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));

        // region NextStep按钮

        /* 设置按钮文字 */
        buttonNS.setText("Next step");
        /* 设置提交事件 */
        buttonNS.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // region 检查填写,选择区域

                /* 检查姓 */
                if (textFieldFirstName.getText().equals("")) {
                    checkMsgErr(textFieldFirstName, "First Name");
                    return;
                } else {
                    setBorder(textFieldFirstName, "First Name");
                }
                /* 检查名 */
                if (textFieldSecondName.getText().equals("")) {
                    checkMsgErr(textFieldSecondName, "Second Name");
                    return;
                } else {
                    setBorder(textFieldSecondName, "Second Name");
                }
                /* 检查邮箱 */
                if (textFieldEmail.getText().equals("")) {
                    checkMsgErr(textFieldEmail);
                    return;
                } else {
                    setBorder(textFieldEmail);
                }
                /* 检查手机号 */
                if (textFieldPhone.getText().equals("")) {
                    checkMsgErr(textFieldPhone);
                    return;
                } else {
                    setBorder(textFieldPhone);
                }
                /* 检查BRC */
                if (textFieldBRC.getText().equals("")) {
                    checkMsgErr(textFieldBRC);
                    return;
                } else {
                    setBorder(textFieldBRC);
                }
                /* 检查选择框RA */
                if (!checkBoxRA.isSelected()) {
                    checkMsgErr(panelRA);
                    return;
                } else {
                    setBorder(panelRA);
                }
                /* 检查选择框CI */
                if (!checkBoxCI.isSelected()) {
                    checkMsgErr(panelCI);
                    return;
                } else {
                    setBorder(panelCI);
                }

                // endregion

                // region 添加信息区域

                /* 添加姓名 */
                UserInformationMap.put("Full Name", textFieldFirstName.getText() + textFieldSecondName.getText());
                UserInformationArray[0] = textFieldFirstName.getText() + textFieldSecondName.getText();
                /* 添加出生年月 */
                UserInformationMap.put("Date of birth", comboBoxMonth.getSelectedItem() + " " + (String) comboBoxYear.getSelectedItem());
                UserInformationArray[1] = comboBoxMonth.getSelectedItem() + " " + (String) comboBoxYear.getSelectedItem();
                /* 选择要添加的课程信息 */
                UserInformationMap.put("Programme Name", comboBoxProgrammeName.getSelectedItem() + ",Year " + (String) comboBoxTerm.getSelectedItem());
                UserInformationArray[2] = comboBoxProgrammeName.getSelectedItem() + ",Year " + (String) comboBoxTerm.getSelectedItem();
                /* 添加邮箱 */
                UserInformationMap.put("Email", textFieldEmail.getText());
                UserInformationArray[3] = textFieldEmail.getText();
                /* 添加手机号 */
                UserInformationMap.put("Phone number", textFieldPhone.getText());
                UserInformationArray[4] = textFieldPhone.getText();
                /* 添加BRC */
                UserInformationMap.put("Book reference code", textFieldBRC.getText());
                UserInformationArray[5] = textFieldBRC.getText();

                // endregion

                // region 最终确认框设置区域

                int status = JOptionPane.showConfirmDialog(panelMain, "Have you checked the information is correct? You can't change the information once the information is submitted.", "Confirm check in?", JOptionPane.YES_NO_OPTION);
                if (status == 1)
                    return;

                // endregion


                /* 启动子组件 */
                AnotherFrame anotherFrame = new AnotherFrame(UserInformationMap);
                anotherFrame.setVisible(true);
            }
        });
        /* 添加到区域 */
        panelBtn.add(buttonNS);

        // endregion

        // region Clear按钮

        /* 设置按钮文字 */
        buttonC.setText("Clear");
        /* 添加清除事件 */
        buttonC.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textFieldFirstName.setText("");
                textFieldSecondName.setText("");
                textFieldEmail.setText("");
                textFieldPhone.setText("");
                textFieldBRC.setText("");
                comboBoxMonth.setSelectedIndex(0);
                comboBoxYear.setSelectedIndex(0);
                comboBoxProgrammeName.setSelectedIndex(0);
                comboBoxTerm.setSelectedIndex(0);
                checkBoxCI.setSelected(false);
                checkBoxRA.setSelected(false);
            }
        });
        /* 添加到区域 */
        panelBtn.add(buttonC);

        // endregion

        // region Exit按钮

        /* 设置按钮文字 */
        buttonE.setText("Exit");
        /* 退出事件 */
        buttonE.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        /* 添加到区域 */
        panelBtn.add(buttonE);

        // endregion

        panelMain.add(panelBtn);

        // endregion

        // region 窗口应用区域
        /* 主区域应用panelMain */
        frameContent.add(panelMain, BorderLayout.CENTER);
        /* 自动调整间距 */
        pack();
        //endregion

    }

    // region 设置边框函数区

    protected void checkMsgErr(JTextField textField, String title) {
        textField.setBorder(new TitledBorder(new LineBorder(Color.red, 1), title));
        JOptionPane.showMessageDialog(this, "Missing information", "Please fill in the missing information.", JOptionPane.ERROR_MESSAGE);
    }

    protected void checkMsgErr(JTextField textField) {
        textField.setBorder(new TitledBorder(new LineBorder(Color.red, 1)));
        JOptionPane.showMessageDialog(this, "Missing information", "Please fill in the missing information.", JOptionPane.ERROR_MESSAGE);
    }

    protected void checkMsgErr(JPanel panel) {
        panel.setBorder(new LineBorder(Color.red, 1));
        JOptionPane.showMessageDialog(this, "Missing information", "Please fill in the missing information.", JOptionPane.ERROR_MESSAGE);
    }


    // endregion

    // region 还原边框函数区

    protected void setBorder(JTextField textField, String title) {
        textField.setBorder(new TitledBorder(title));
    }

    protected void setBorder(JTextField textField) {
        textField.setBorder(new EmptyBorder(0, 0, 0, 0));
    }

    protected void setBorder(JPanel panel) {
        panel.setBorder(new EmptyBorder(0, 0, 0, 0));
    }


    // endregion

    /* 用户信息map格式 */
    protected Map<String, String> UserInformationMap = new LinkedHashMap<>();
    /* 用户信息array格式 */
    protected String[] UserInformationArray = new String[6];

}
